package cobersky.skinUI.components
{
	import cobersky.skinUI.core.Component;
	import cobersky.skinUI.core.GlobalEventManager;
	import cobersky.skinUI.skins.core.IBarSkin;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	[Event(name="change", type="flash.events.Event")]
	public class BarBase extends Component
	{
		protected var _thumb:Button;
		protected var _back:DisplayObject;
		protected var _value:Number=0;
		public function BarBase()
		{
		}
		
		override protected function setEnable():void
		{
			_thumb.visible=true;
			super.setEnable();
		}
		
		override protected function setUnable():void
		{
			trace("234234234");
			_thumb.visible=false;
			super.setUnable();
		}
		
		override public function attachSkin():void{
			super.attachSkin();
			var b:IBarSkin=new _skinClass();
			_thumb=b.thumb as Button;
			_back=b.back;
			addChild(_back);
			addChild(_thumb);
			this.addEventListener(MouseEvent.MOUSE_DOWN,mouseEventHandler);
		}
		protected var dragRect:Rectangle;
		private function mouseEventHandler(event:MouseEvent):void
		{
			if(event.target==_thumb){
				stage.addEventListener(MouseEvent.MOUSE_UP,onUp);
				stage.addEventListener(MouseEvent.MOUSE_MOVE,onMove);
				_thumb.startDrag(false,dragRect);
			}else{
				backClick();
			}
		}
		protected function backClick():void{}
		protected function onMove(event:MouseEvent):void{
			_thumbHasMove=false
		}
		
		private function onUp(e:MouseEvent):void{
			stage.removeEventListener(MouseEvent.MOUSE_UP,onUp);
			stage.removeEventListener(MouseEvent.MOUSE_MOVE,onMove);
			_thumb.stopDrag();
		}
		public function get value():Number
		{
			return _value;
		}
		protected var _thumbHasMove:Boolean;
		public function set value(value:Number):void{
			if(_value==value)return;
			_value = Math.min(Math.max(value,0),1);
			if(!_thumbHasMove){
				moveThumb();
			}
			_thumbHasMove=false;
			this.dispatchEvent(new Event("change"));
		}
		
		protected function moveThumb():void{
			
		}
	}
}